<template>
  <dv-full-screen-container class="bg">
    <div class="register-container">
      <dv-border-box-12 class="register-box" :color="['#00ffd9', 'rgba(0, 255, 217, 0.3)']">
        <div class="register-content">
          <div class="register-header">
            <h2>智慧大棚管理系统</h2>
          </div>
          <div class="register-form">
            <div class="form-item">
              <div class="input-wrapper">
                <i class="iconfont icon-user"></i>
                <input
                    type="text"
                    v-model="registerForm.username"
                    placeholder="请输入用户名"
                >
              </div>
            </div>
            <div class="form-item">
              <div class="input-wrapper">
                <i class="iconfont icon-user"></i>
                <input
                    type="text"
                    v-model="registerForm.name"
                    placeholder="请输入姓名"
                >
              </div>
            </div>
            <div class="form-item">
              <div class="input-wrapper">
                <i class="iconfont icon-password"></i>
                <input
                    type="password"
                    v-model="registerForm.password"
                    placeholder="请输入密码"
                >
              </div>
            </div>
            <div class="form-item">
              <div class="input-wrapper">
                <i class="iconfont icon-password"></i>
                <input
                    type="password"
                    v-model="registerForm.confirmPassword"
                    placeholder="请确认密码"
                >
              </div>
            </div>
            <div class="form-item">
              <dv-border-box-12 class="register-button" @click="handleRegister">
                <span>立即注册</span>
              </dv-border-box-12>
            </div>
            <div class="login-link">
              已有账号？
              <el-button type="text" @click="goToLogin">返回登录</el-button>
            </div>
          </div>
        </div>
      </dv-border-box-12>
    </div>
  </dv-full-screen-container>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { register } from '@/api/user'

const router = useRouter()
const registerForm = reactive({
  username: '',
  name: '',
  password: '',
  confirmPassword: ''
})

const handleRegister = async () => {
  // 表单验证
  if (!registerForm.username || !registerForm.name || !registerForm.password || !registerForm.confirmPassword) {
    ElMessage.error('请填写所有必填项');
    return;
  }
  if (registerForm.password !== registerForm.confirmPassword) {
    ElMessage.error('两次输入的密码不一致');
    return;
  }
  if (registerForm.password.length < 6) {
    ElMessage.error('密码长度不能少于6位');
    return;
  }

  try {
    const res = await register({
      username: registerForm.username,
      name: registerForm.name,
      password: registerForm.password
    })
    
    if (res.code === 200) {
      ElMessage.success('注册成功')
      router.push('/login')
    } else {
      ElMessage.error(res.message || '注册失败')
    }
  } catch (error) {
    ElMessage.error(error.message || '注册失败')
  }
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.bg {
  width: 100%;
  height: 100%;
  padding: 16px 16px 0 16px;
  background-image: url('@/assets/img.png');
  background-size: cover;
  background-position: center center;
}

.register-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.register-box {
  width: 400px;
  height: 500px;
  padding: 20px;
}

.register-content {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: rgba(0, 28, 36, 0.5);
  border-radius: 4px;
  padding: 20px;
}

.register-header {
  margin-bottom: 30px;
}

.register-header h2 {
  color: #00ffd9;
  font-size: 24px;
  text-align: center;
  text-shadow: 0 0 10px rgba(0, 255, 217, 0.3);
}

.register-form {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-item {
  width: 100%;
}

.input-wrapper {
  width: 100%;
  height: 40px;
  display: flex;
  align-items: center;
  background: rgba(0, 28, 36, 0.3);
  border-radius: 4px;
  padding: 0 10px;
  border: 1px solid rgba(0, 255, 217, 0.3);
}

.input-wrapper i {
  color: #00ffd9;
  font-size: 20px;
  margin-right: 10px;
}

.input-wrapper input {
  flex: 1;
  height: 100%;
  background: transparent;
  border: none;
  outline: none;
  color: #fff;
  font-size: 16px;
}

.input-wrapper input::placeholder {
  color: rgba(255, 255, 255, 0.5);
}

.register-button {
  --border-color: #00ffd9;
  --border-alpha: 0.3;
  width: 100%;
  height: 45px;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
}

.register-button span {
  color: #00ffd9;
  font-size: 20px;
  font-weight: bold;
  display: inline-block;
  text-align: center;
  margin-top: 4px;
  width: 100%;
  text-shadow: 0 0 10px rgba(0, 255, 217, 0.3);
}

.register-button:hover span {
  color: #fff;
  text-shadow: 0 0 15px rgba(0, 255, 217, 0.5);
}

.login-link {
  text-align: center;
  margin-top: 15px;
  color: #00ffd9;
}

.login-link .el-button {
  color: #00ffd9;
  font-weight: bold;
}

.login-link .el-button:hover {
  color: #fff;
  text-shadow: 0 0 10px rgba(0, 255, 217, 0.3);
}
</style> 